// register.js
import util from '../../../utils/util.js';

import dateTimePicker from'../../../utils/dateTimePicker.js';
var logincontent = '尚无爱车，烦请先添加';
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: '1', value: '洗车' ,checked: 'true' },
      { name: '2', value: '保养' },
      { name: '3', value: '钣金' },
      { name: '4', value: '喷漆' },
      { name: '5', value: '维修' },
    ],
    shopinfo: '',
    carinfo:'',
    orderservicetype:1,

    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2000,
    endYear: 2050
     
  },
  bindDateChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      buytime: e.detail.value
    })
  },


  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      orderservicetype : e.detail.value,
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "我的预定"
    });

    let data = JSON.parse(options.data);
   
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop()

    this.setData({
      shopinfo: data,
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
    
   
    
  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getcarlist();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    
    this.getcarlist();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
  getcarlist: function ()  {
    var url = app.globalData.SERVER+'car/getAllMyCars';

    // var tel = xcx_user_info.tel;
    // var tel = user.tel;
    // var userinfo = wx.getStorageSync("user_info");
    var userinfo = util.getData("userinfo");
    var tel = userinfo.tel;
    var i = 1;
    util.request(url, 'POST', {

      tel: tel,
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1) {
        for(var i=0;i<err.data.ret.length;i++)
        {
          if(err.data.ret[i].isdefault == '1')
          {
            this.setData({
              carinfo: err.data.ret[i],
            });
            break;
          }

          
        }
        
        var num = 1;
      } else {
        self.setData({
          siteInfo: err.data.site_info
        });
      }


    });
  },

  formSubmit(event) {
    if(this.data.carinfo == '')
    {
      wx.showModal({
        title: '提醒',
        content: logincontent,
        success: function (eci) {
          if (eci.confirm) {
            wx.navigateTo({
              url: '../../mycar/addcar/mycar'
            })
          } else {
          }
        }
      });
    }else{


    var url = app.globalData.SERVER+'contact/addOrderRepair2';
    var eValue = event.detail.value;

    var year = 2000+this.data.dateTime1[0];
    var month =   this.data.dateTime1[1] + 1;
   
    month = month < 10 ? ('0' + month) : month;
    var day = this.data.dateTime1[2] + 1;
    day = day < 10 ? ('0' + day) : day;

    var hour = this.data.dateTime1[3] ;
    hour = hour < 10 ? ('0' + hour) : hour;

    var min = this.data.dateTime1[4];
    min = min < 10 ? ('0' + min) : min;
    
    var time = year + '-' + month + "-" + day + ' ' + hour + ':' + min+':00'; 
      if (eValue == undefined){
        return
      }
    var content = eValue.info;
    // var usetinfo = wx.getStorageSync("user_info");
      var usetinfo = util.getData("userinfo");
    var tel = usetinfo.tel;
    var that = this;
    var shop = this.data.shopinfo;
    var car = this.data.carinfo;

    var usetinfo_id = usetinfo._id;
    var carer_id = car._id;
    var shop_id = shop._id;
    util.request(url, 'POST', {
      openid: app.globalData.openId,
      time: time,
      info: content,
      orderservicetype: this.data.orderservicetype,
      owner: shop.tel,
      carer: usetinfo_id,
      carerid: usetinfo_id,
      car: carer_id,
      carid: carer_id,
      shop: shop_id,
      shopid: shop_id,
      
    }).then(res => {
      util.showToast('新增成功', 'error', 1000);
    }).catch(err => {
      if (err.data.code === 1) {
        util.showToast('预约成功', 'success', 1000);
        setTimeout(function () {
          wx.navigateBack({
            delta: -1
          });
        }, 2000) //延迟时间 这里是1秒
      } else {
       
      }
  
    });
    }
  },



  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
  },
  changeDateTimeColumn(e){
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({ 
      dateTimeArray1: dateArr
    });
  }

});